package com.boatAnchorAssistant.service.repository.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.boatAnchorAssistant.pojo.entity.DataDictionary;
import com.boatAnchorAssistant.service.mapper.DataDictionaryMapper;
import com.boatAnchorAssistant.service.repository.in.DataDictionaryRepository;
import nirvana.core.exceptions.BizException;
import nirvana.core.repository.Repository;
import org.springframework.stereotype.Service;
import com.boatAnchorAssistant.pojo.dto.dataDictionary.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 */
@Service
public class DataDictionaryRepositoryImpl extends Repository<DataDictionaryMapper, DataDictionary> implements DataDictionaryRepository {

    private final DataDictionaryMapper mapper;

    public DataDictionaryRepositoryImpl(DataDictionaryMapper mapper) {
        this.mapper = mapper;
    }


    @Override
    public DataDictionary createToBefore(DataDictionary dataDictionary) {

        String code = dataDictionary.getCode();
        String name = dataDictionary.getName();
        Integer displayOrder = dataDictionary.getDisplayOrder();
        Long type = dataDictionary.getType();


        DataDictionary po = null;
        DataDictionary byPo = null;


        po = new DataDictionary();
        po.setType(type);
        po.setCode(code);
        byPo = getByEntity(po);

        if (byPo != null) {
            throw new BizException("数据字典编号不能重复");
        }


        po = new DataDictionary();
        po.setType(type);
        po.setName(name);
        byPo = getByEntity(po);

        if (byPo != null) {
            throw new BizException("数据字典名称不能重复");
        }


        po = new DataDictionary();
        po.setType(type);
        po.setDisplayOrder(displayOrder);
        byPo = getByEntity(po);

        if (byPo != null) {
            throw new BizException("数据字典排序不能重复");
        }

        return super.createToBefore(dataDictionary);
    }


    @Override
    public DataDictionary editToBefore(DataDictionary dataDictionary) {

        String code = dataDictionary.getCode();
        String name = dataDictionary.getName();
        Integer displayOrder = dataDictionary.getDisplayOrder();
        Long type = dataDictionary.getType();
        Long id = dataDictionary.getId();


        DataDictionary po = null;
        DataDictionary byPo = null;

        po = new DataDictionary();
        po.setCode(code);
        po.setType(type);
        byPo = getByEntity(po);

        if (byPo != null && !id.equals(byPo.getId())) {
            throw new BizException("数据字典编号不能重复");
        }


        po = new DataDictionary();
        po.setType(type);
        po.setName(name);
        byPo = getByEntity(po);

        if (byPo != null && !id.equals(byPo.getId())) {
            throw new BizException("数据字典名称不能重复");
        }


        po = new DataDictionary();
        po.setType(type);
        po.setDisplayOrder(displayOrder);
        byPo = getByEntity(po);

        if (byPo != null && !id.equals(byPo.getId())) {
            throw new BizException("数据字典排序不能重复");
        }

        return super.editToBefore(dataDictionary);
    }

    /**
     * 搜索数据字典
     *
     * @param dto
     * @return
     */
    @Override
    public Page<DataDictionary> selectDataDictionary(SelectDataDictionaryDto dto) {

        return mapper.selectDataDictionary(dto);
    }

    /**
     * 检查这个类型下是否存在这个编号
     *
     * @param code
     * @param type
     * @return
     */
    @Override
    public Boolean hasCode(String code, Long type) {
        DataDictionary t = new DataDictionary();
        t.setCode(code);
        t.setType(type);
        return getByEntity(t) != null;
    }

    /**
     * 获取排序最后的字典
     *
     * @param type
     * @return
     */
    @Override
    public DataDictionary getLastDisplay(Long type) {
        return mapper.getLastDisplay(type);
    }

    /**
     * 获取某个排序之后的所有字典
     *
     * @param type
     * @param displayOrder
     * @return
     */
    @Override
    public List<DataDictionary> getAfterListByOrder(Long type, Integer displayOrder) {
        return mapper.getAfterListByOrder(type,displayOrder);
    }

    @Override
    public Map<Long, DataDictionary> getMap(List<Long> ids) {
        Map<Long, DataDictionary> map = new HashMap<>();

        if (CollectionUtil.isNotEmpty(ids)) {
            List<DataDictionary> list =listByIds(ids);
            if (CollectionUtil.isNotEmpty(list)) {
                map = list.stream().collect(Collectors.toMap(DataDictionary::getId, pojo -> pojo));
            }
        }
        return map;
    }

}